<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Scholar extends SMB_Controller {
	
	public function __construct() {
		parent::__construct();
		$this->load->library('scholarship');
	}
	
	public function index() {
		$title = 'Daftar Beasiswa Ditawarkan';
		$this->_common_scholar_list(5, PENDAFTARAN, 'index', $title);
	}
	
	public function selection() {
		$title = 'Daftar Seleksi Beasiswa';
		$this->_common_scholar_list(5, SELEKSI, 'selection', $title);
	}
	
	public function active() {
		$title = 'Daftar Beasiswa Aktif';
		$this->_common_scholar_list(6, AKTIF, 'active', $title);
	}
	
	public function nonactive() {
		$title = 'Daftar Beasiswa Non-Aktif';
		$this->_common_scholar_list(5, NONAKTIF, 'nonactive', $title);
	}
	
	private function _common_scholar_list($pager, $type, $url, $title) {
		$this->_get_userinfo();
		$this->load->helper('table');		
		$limit = $this->scholarship->count_provided_scholarship($this->userinfo['username'], $type);
		
		$this->_load_common_header();
		$this->_load_common_navigation();
		$this->_load_common_sidebar(get_user_sidebar($this->userinfo['role'], 'scholarship'));
		$params = array(
			'scholarships' => $this->scholarship->get_provided_scholarship($this->userinfo['username'], '0', $limit, $type),
			'title' => $title,
			'url' => $url,
			'pager' => get_pager($pager)
		);
		$this->load->view('sponsor/scholarship_list', $params);
		$this->_load_common_footer();
	}
	
	public function detail($id = NULL) {
		$this->_get_userinfo();
		$this->load->helper('table'); 
		$scholarship = $this->_validate_scholar_id($id, $this->userinfo['username']);
		
		$start = strtotime($scholarship->period_start);
		$end = strtotime($scholarship->period_end);
		$deadline = strtotime($scholarship->deadline);
		$today = strtotime('today');
		if ($today <= $deadline) {
			$status = 'masa pendaftaran';
			$title = 'Daftar Mahasiswa Pendaftar Beasiswa';
			$empty = 'Tidak ada mahasiswa mendaftar beasiswa ini.';
			$studentstatus = 'Status Pendaftaran';
			$countertitle = 'Jumlah Pendaftar';
			$limit = $this->scholarship->count_reciever_scholarship($scholarship->id, PENDAFTARAN);
			$recievers = $this->scholarship->get_reciever_scholarship($id, '0', $limit, PENDAFTARAN);
			$counter = $limit;
		} else if ($deadline < $today && $today <= $start) {
			$status = 'masa seleksi';
			$title = 'Daftar Mahasiswa Pendaftar Beasiswa';
			$empty = 'Tidak ada mahasiswa mendaftar beasiswa ini.';
			$studentstatus = 'Status Pendaftaran';
			$countertitle = 'Jumlah Pendaftar';
			$limit = $this->scholarship->count_reciever_scholarship($scholarship->id, SELEKSI);
			$recievers = $this->scholarship->get_reciever_scholarship($id, '0', $limit, SELEKSI);
			$counter = $this->scholarship->get_selection_counter($id);
		} else if ($start < $today && $today <= $end) {
			$status = 'aktif';
			$title = 'Daftar Mahasiswa Penerima Beasiswa';
			$empty = 'Tidak ada mahasiswa menerima beasiswa ini.';
			$studentstatus = 'Status Pengiriman';
			$countertitle = 'Jumlah Penerima';
			$limit = $this->scholarship->count_reciever_scholarship($scholarship->id, AKTIF);
			$recievers = $this->scholarship->get_reciever_scholarship($id, '0', $limit, AKTIF);
			$counter = $limit;
		} else {
			$status = 'non-aktif';
			$title = 'Daftar Mahasiswa Penerima Beasiswa';
			$empty = 'Tidak ada mahasiswa menerima beasiswa ini.';
			$studentstatus = 'Status Pengiriman';
			$countertitle = 'Jumlah Penerima';
			$limit = $this->scholarship->count_reciever_scholarship($scholarship->id, NONAKTIF);
			$recievers = $this->scholarship->get_reciever_scholarship($id, '0', $limit, NONAKTIF);
			$counter = $limit;
		}

		$this->_load_common_header();
		$this->_load_common_navigation();
		$this->_load_common_sidebar(get_user_sidebar($this->userinfo['role'], 'scholarship'));
		$params = array(
			'offset' => '0',
			'recievers' => $recievers, 
			'scholarship' => $scholarship,
			'status' => $status,
			'title' => $title,
			'empty' => $empty,
			'studentstatus' => $studentstatus,
			'counter' => $counter,
			'pager' => get_pager(4)
		);
		$this->load->view('sponsor/scholarship_detail', $params);
		$this->_load_common_footer();
	}
	
	private function _validate_scholar_id($id, $sponsor) {
		if ($id == NULL) {
			redirect('sponsor/scholar/index', 'location');
			exit;
		}
		$scholarship = $this->scholarship->get_scholarship_by_id_and_sponsor($id, $sponsor);
		if ($scholarship == NULL) {
			redirect('sponsor/scholar/index', 'location');
			exit;
		} else {
			return $scholarship;
		}
	}
}